package com.felix.netty.c1.future;

import io.netty.channel.DefaultEventLoop;
import io.netty.util.concurrent.DefaultPromise;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;


/** await 死锁检查 **/
@Slf4j
public class example6 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        DefaultEventLoop eventExecutors = new DefaultEventLoop();
        DefaultPromise<Object> promise = new DefaultPromise<>(eventExecutors);

        eventExecutors.submit(()->{

            System.out.println("1");

            // 注意不能仅捕获 InterruptedException 异常
            // 否则 死锁检查抛出的 BlockingOperationException 会继续向上传播
            // 而提交的任务会被包装为 PromiseTask，它的 run 方法中会 catch 所有异常然后设置为 Promise 的失败结果而不会抛出
            try {
                promise.await();
            } catch (Exception e) {
                e.printStackTrace();
            }

            System.out.println("2");
        });

        eventExecutors.submit(()->{

            System.out.println("3");

            try {
                promise.await();
            } catch (Exception e) {
                e.printStackTrace();
            }

            System.out.println("4");
        });
    }
}
